package com.CGFrame.struts.bean.project;

import java.io.File;
import java.text.NumberFormat;
import java.util.regex.Pattern;

import com.CGFrame.struts.POJOs.Elementgroup;
import com.CGFrame.struts.POJOs.Project;
import com.CGFrame.struts.POJOs.Shot;
import com.CGFrame.struts.POJOs.Shotgroup;
import com.CGFrame.struts.POJOs.Status;
import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;

public class ProjectFileLogic {

	public void makeDir(ProjectFileBean prbean) {

		String espResult = prbean.getEspResult();
		String scenceResult = prbean.getScenceResult();
		String shotResult = prbean.getShotResult();
		String[] espArray = null;
		String[] scencetmp = null;
		String[] shottmp = null;
		if (prbean.isEspCheck()) {
			espArray = espResult.split(";");
		}
		if (prbean.isScenceCheck()) {
			scencetmp = scenceResult.split(";");
		}
		shottmp = shotResult.split(";");

		String fs = System.getProperties().getProperty("file.separator");

		String path = prbean.getFilePath();
		if (path.indexOf(fs) == -1) {
			path += fs;
		}
		path += prbean.getProjectShortName() + fs + "Project" + fs + "scenes"
				+ fs + "Episodes" + fs;

		if (prbean.isEspCheck()) {// 有集
			for (int i = 0; i < espArray.length; i++) {

				String espPath = path
						+ getName(prbean.getEspName(), espArray[i]) + fs
						+ "Production" + fs;
				int shotnum = 1;

				String[] shotArray = null;
				if (prbean.isScenceCheck()) {
					shotArray = shottmp[i].split(",");
				} else {
					shotArray = shottmp;
				}
				if (prbean.isScenceCheck()) { // 有场
					String scencePath = null;
					String[] scenceArray = scencetmp[i].split(",");

					if (prbean.isSingleShot()) { // 场独立
						for (int k = 0; k < scenceArray.length; k++) {
							scencePath = espPath
									+ getName(prbean.getScenceName(),
											scenceArray[k]);
							for (int j = 0; j < new Integer(shotArray[k]); j++) {
								if (prbean.isContShot()) { // 有集、有场、场独立、镜头连续
									mkdictory(scencePath
											+ fs
											+ prbean.getProjectShortName()
											+ _getName(prbean.getEspName(),
													espArray[i])
											+ _getName(prbean.getScenceName(),
													scenceArray[k])
											+ _getName(prbean.getShotName(),
													String.valueOf(shotnum)));
									shotnum++;
								} else { // 有集、有场、场独立、镜头不连续
									mkdictory(scencePath
											+ fs
											+ prbean.getProjectShortName()
											+ _getName(prbean.getEspName(),
													espArray[i])
											+ _getName(prbean.getScenceName(),
													scenceArray[k])
											+ _getName(prbean.getShotName(),
													String.valueOf(j + 1)));
								} // if~else~
							}
						}
					} else { // 场不独立
						scencePath = espPath + "Shot" + fs;
						for (int k = 0; k < scenceArray.length; k++) {
							for (int j = 0; j < new Integer(shotArray[k]); j++) {
								if (prbean.isContShot()) { // 有集、有场、场不独立、镜头连续
									mkdictory(scencePath
											+ fs
											+ prbean.getProjectShortName()
											+ _getName(prbean.getEspName(),
													espArray[i])
											+ _getName(prbean.getScenceName(),
													scenceArray[k])
											+ _getName(prbean.getShotName(),
													String.valueOf(shotnum)));
									shotnum++;
								} else { // 有集、有场、场不独立、镜头不连续
									mkdictory(scencePath
											+ fs
											+ prbean.getProjectShortName()
											+ _getName(prbean.getEspName(),
													espArray[i])
											+ _getName(prbean.getScenceName(),
													scenceArray[k])
											+ _getName(prbean.getShotName(),
													String.valueOf(j + 1)));
								} // if~else~
							}
						}

					}// if ~ else ~

				} else { // 没场
					for (int j = 0; j < new Integer(shotArray[i]); j++) {
						if (prbean.isContShot()) { // 有集、没场、 镜头连续
							String shotPath = espPath + fs + "Shot" + fs;
							mkdictory(shotPath
									+ prbean.getProjectShortName()
									+ _getName(prbean.getEspName(), espArray[i])
									+ _getName(prbean.getShotName(), String
											.valueOf(shotnum)));
							shotnum++;
						}
					}
				} // if~else~
			}// 集 ~for
		} else {// 没集
			String espPath = path + fs + "Production" + fs;
			int shotnum = 1;
			if (prbean.isScenceCheck()) { // 有场
				String scencePath = null;
				String[] scenceArray = scencetmp;
				String[] shotArray = shottmp;

				if (prbean.isSingleShot()) { // 场独立
					for (int k = 0; k < scenceArray.length; k++) {
						scencePath = espPath
								+ getName(prbean.getScenceName(),
										scenceArray[k]);
						for (int j = 0; j < new Integer(shotArray[k]); j++) {
							if (prbean.isContShot()) { // 没集、有场、场独立、镜头连续
								mkdictory(scencePath
										+ fs
										+ prbean.getProjectShortName()
										+ _getName(prbean.getScenceName(),
												scenceArray[k])
										+ _getName(prbean.getShotName(), String
												.valueOf(shotnum)));
								shotnum++;
							} else { // 没集、有场、场独立、镜头不连续
								mkdictory(scencePath
										+ fs
										+ prbean.getProjectShortName()
										+ _getName(prbean.getScenceName(),
												scenceArray[k])
										+ _getName(prbean.getShotName(), String
												.valueOf(j + 1)));
							} // if~else~
						}
					}
				} else { // 场不独立
					scencePath = espPath + "Shot" + fs;
					for (int k = 0; k < scenceArray.length; k++) {
						for (int j = 0; j < new Integer(shotArray[k]); j++) {
							if (prbean.isContShot()) { // 没集、有场、场不独立、镜头连续
								mkdictory(scencePath
										+ fs
										+ prbean.getProjectShortName()
										+ _getName(prbean.getScenceName(),
												scenceArray[k])
										+ _getName(prbean.getShotName(), String
												.valueOf(shotnum)));
								shotnum++;
							} else { // 没集、有场、场不独立、镜头不连续
								mkdictory(scencePath
										+ fs
										+ prbean.getProjectShortName()
										+ _getName(prbean.getScenceName(),
												scenceArray[k])
										+ _getName(prbean.getShotName(), String
												.valueOf(j + 1)));
							} // if~else~
						}
					}

				}// if ~ else ~

			} else { // 没集、没场、 镜头连续
				int num = 1;
				String shotPath = espPath + fs + "Shot" + fs;
				for (int i = 0; i < shottmp.length; i++) {
					for (int j = 0; j < new Integer(shottmp[i]); j++) {
						mkdictory(shotPath
								+ prbean.getProjectShortName()
								+ _getName(prbean.getShotName(), String
										.valueOf(num)));
						num++;
					} // ~for~
				}
			} // if~else~
		}// if~ else~~~

	}

	public void savePrbean(ProjectFileBean prbean) {

		ProjectOperate poper = ProjectOperateFactory.getprojectOperate();

		String esp = prbean.getEspName();
		String espstr = prbean.getEspResult();
		String sce = prbean.getScenceName();
		String scestr = prbean.getScenceResult();
		String shot = prbean.getShotName();
		String shotstr = prbean.getShotResult();
		String[] espArr = null;
		if (prbean.isEspCheck()) {
			espArr = espstr.split(";");
		}
		String[] sceArr = null;
		if (prbean.isScenceCheck()) {
			sceArr = scestr.split(";");
		}
		String[] shotArr = shotstr.split(";");

		Status projectStatus = (Status) poper.UniqueQuery("Status", "statusId," + "8");
		Status shotStatus = (Status)poper.UniqueQuery("Status", "statusId," + "11");
		Status elementStatus = (Status)poper.UniqueQuery("Status", "statusId," + "21");
		
		Project project = prbean.getProject();
		project.setStatus(projectStatus);

		poper.saveObject(project);
		
		Shotgroup sg = new Shotgroup();
		sg.setShotGroupName("Episodes");
		sg.setProject(project);
		sg.setStatus(elementStatus);
		poper.saveObject(sg);
		Elementgroup eg = new Elementgroup();
		eg.setElementGroupName("Assets");
		eg.setProject(project);
		eg.setStatus(elementStatus);
		poper.saveObject(eg);
		
		if (prbean.isEspCheck()) {// 有集
			for (int i = 0; i < espArr.length; i++) {
				Shotgroup shp = new Shotgroup();
				String espName = getName(esp, espArr[i]);
				shp.setShotGroupName(espName);
				shp.setProject(project);
				shp.setShotgroup(sg);
				shp.setStatus(elementStatus);
				poper.saveObject(shp);
				if (prbean.isScenceCheck()) {// 有集，有场
					String[] espSceShotArr = shotArr[i].split(",");
					String[] espSceArr = sceArr[i].split(",");
					for (int j = 0; j < espSceArr.length; j++) {
						String sceName = getName(sce, espSceArr[j]);
						Shotgroup sceshp = new Shotgroup();
						sceshp.setShotGroupName(sceName);
						sceshp.setProject(project);
						sceshp.setShotgroup(shp);
						sceshp.setStatus(elementStatus);
						poper.saveObject(sceshp);

						for (int k = 0; k < new Integer(espSceShotArr[j]); k++) {
							String shotName = prbean.getProjectShortName()
									+ "_" + espName + "_" + sceName + "_"
									+ getName(shot, String.valueOf(k+1));
							Shot shotob = new Shot();
							shotob.setShotName(shotName);
							shotob.setShotgroup(sceshp);
							shotob.setStatus(shotStatus);
							poper.saveObject(shotob);
						}
					}
				} else {
					for (int k = 0; k < new Integer(shotArr[i]); k++) {
						String shotName = prbean.getProjectShortName() + "_"
								+ espName + "_"
								+ getName(shot, String.valueOf(k+1));
						Shot shotob = new Shot();
						shotob.setShotName(shotName);
						shotob.setShotgroup(shp);
						shotob.setStatus(shotStatus);
						poper.saveObject(shotob);
					}

				}
			}
		} else {// 没集
			if (prbean.isScenceCheck()) {// 没集，有场
				for (int j = 0; j < sceArr.length; j++) {
					String sceName = getName(sce, sceArr[j]);
					Shotgroup sceshp = new Shotgroup();
					sceshp.setShotGroupName(sceName);
					sceshp.setProject(project);
					sceshp.setShotgroup(sg);
					sceshp.setStatus(elementStatus);
					poper.saveObject(sceshp);

					for (int k = 0; k < new Integer(shotArr[j]); k++) {
						String shotName = prbean.getProjectShortName() + "_"
								+ sceName + "_"
								+ getName(shot, String.valueOf(k+1));
						Shot shotob = new Shot();
						shotob.setShotName(shotName);
						shotob.setShotgroup(sceshp);
						shotob.setStatus(shotStatus);
						poper.saveObject(shotob);
					}

				}
			} else {
				for (int i = 0; i < shotArr.length; i++) {
					int num = 1;
					for (int k = 0; k < new Integer(shotArr[i]); k++) {
						String shotName = prbean.getProjectShortName() + "_"
								+ getName(shot, String.valueOf(num));
						Shot shotob = new Shot();
						shotob.setShotName(shotName);
						shotob.setShotgroup(sg);
						shotob.setStatus(elementStatus);
						poper.saveObject(shotob);
						num++;
					}
				}
			}

		}
	}

	// _esp000, 1 变成 esp001
	private String getName(String esp, String numstr) {
		String str1 = nameFormat(esp);
		String str2 = numFormat(new Integer(numstr), getNameNum(esp));
		return str1 + str2;
	}

	// _esp000, 1 变成 _esp001
	private String _getName(String esp, String numstr) {
		String str1 = nameFormat(esp);
		String str2 = numFormat(new Integer(numstr), getNameNum(esp));
		return "_" + str1 + str2;
	}

	private File mkdictory(String fileName) {
		File file = new File(fileName);
		if (!file.exists()) {
			file.mkdirs();
		}
		return file;
	}

	private String numFormat(int value, int num) {
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMinimumIntegerDigits(num);
		return nf.format(value);
	}

	private int getNameNum(String name) {
		Pattern pattern = Pattern.compile("[0-9]*");
		boolean b = true;
		int i = name.length() - 1;
		int num = 0;
		while (b) {
			char ch = name.charAt(i);
			b = pattern.matcher(String.valueOf(ch)).matches();
			i--;
			num++;
		}
		return --num;

	}

	// 把 _name000 变成 name
	private String nameFormat(String name) {
		String tmp = null;
		if (name.indexOf('_') == 0) {
			tmp = name.substring(1, name.length());
		} else {
			tmp = name;
		}
		int num = getNameNum(tmp);
		return tmp.substring(0, name.length() - num - 1);
	}

	public static void main(String[] args) throws Exception {
		ProjectFileLogic pl = new ProjectFileLogic();
		ProjectFileBean prbean = new ProjectFileBean();
		prbean.setProjectShortName("Test10");
		prbean.setFilePath("d:\\");
		prbean.setEspCheck(true);
		prbean.setScenceCheck(true);
		prbean.setEspName("_esp000");
		prbean.setScenceName("_sce000");
		prbean.setShotName("_shot00");
		prbean.setAllName("Test_esp000_sce000_shot00");
		prbean.setSingleShot(true);
		prbean.setContShot(true);
		prbean.setEspResult("1;2;");
		prbean.setScenceResult("2;3;");
		prbean.setShotResult("2;3;");
		pl.makeDir(prbean);
	}

}
